﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CognisessService;
using CognisessDA;
using CognisessCommLib;
using Webdiyer.WebControls.Mvc;

namespace CognisessWeb.Controllers
{
    public class CareerPositionController : Controller
    {
        AccountBLL accountBLL = new AccountBLL();

        #region Action Methods
        #region Get
        // GET: /CareerPosition/
        public ActionResult Index(int? id)
        {

            PagedList<CareerPosition> result = accountBLL.GetPagingCareerPosition(new CareerPosition()).ToPagedList(id ?? 1, 10);
            if (Request.IsAjaxRequest())
                return PartialView("CareerPositionList", result);
            return View(result);
        }

        // GET: /CareerPosition/Details/5
        public ActionResult Details(int id)
        {
            CareerPosition result = accountBLL.GetCareerPosition(new CareerPosition { Id = id }).FirstOrDefault();
            this.SetDDLIsValid(result.IsVaild);
            return View(result);
        }

        // GET: /CareerPosition/Create
        public ActionResult Create()
        {
            this.SetDDLIsValid(true);
            return View();
        }

        // GET: /CareerPosition/Edit/5
        public ActionResult Edit(int id)
        {
            CareerPosition result = accountBLL.GetCareerPosition(new CareerPosition { Id = id }).FirstOrDefault();
            this.SetDDLIsValid(result.IsVaild);
            return View(result);
        }

        // GET: /CareerPosition/Delete/5
        public ActionResult Delete(int id)
        {
            bool isSuccessful = accountBLL.DeleteCareerPosition(id);
            if (isSuccessful)
            {
                TempData["ExcuteSuccfull"] = "Delete Successfully.";
            }
            else
            {
                TempData["ExcuteSuccfull"] = "Operation fail. Please contact with the administrator.";
            }
            return RedirectToAction("Index");
        }
        #endregion

        #region Post
        // POST: /CareerPosition/Create
        [HttpPost]
        public ActionResult Create(CareerPosition collection)
        {
            try
            {
                if (accountBLL.GetCareerPosition(new CareerPosition { Name = collection.Name }).Count > 0)
                {
                    ViewData.ModelState.AddModelError("Name", ConstantHelper.ERROR_NAME_EXIST);
                }
                if (ViewData.ModelState.IsValid)
                {
                    accountBLL.AddCareerPosition(new List<CareerPosition> { collection });
                    return RedirectToAction("Index");
                }
                this.SetDDLIsValid(collection.IsVaild);
                return View();

            }
            catch
            {
                return View();
            }
        }

        // POST: /CareerPosition/Edit/5
        [HttpPost]
        public ActionResult Edit(int id, CareerPosition collection)
        {
            try
            {
                if (accountBLL.GetCareerPosition(new CareerPosition { Name = collection.Name }).Count > 0)
                {
                    ViewData.ModelState.AddModelError("Name", ConstantHelper.ERROR_USER_EXIST);
                }
                if (ViewData.ModelState.IsValid)
                {
                    accountBLL.UpdataCareerPosition(new List<CareerPosition> { collection });
                    return RedirectToAction("Index");
                }
                this.SetDDLIsValid(collection.IsVaild);
                return View(collection);

            }
            catch
            {
                return View();
            }
        }

        // POST: /CareerPosition/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        #endregion
        #endregion

        #region Private Method
        private void SetDDLIsValid(bool isValid)
        {
            ViewData["IsValid"] = new SelectList(XMLHelper.GetOptions("IsValid").Select(
                                                                                           o =>  new 
                                                                                           {
                                                                                               Text =o.Text , Value = !Convert.ToBoolean(o.Value) 
                                                                                            }
                                                                                         ),
                                                  "Value","Text",isValid
                                                  );
        }
        #endregion
    }
}
